package com.tjvib.presenter;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.ResultReceiver;
import android.widget.Toast;

import com.tjvib.bean.SensorInfo;
import com.tjvib.common.Callback;
import com.tjvib.service.LpmsB2SensorService;
import com.tjvib.service.SensorService;
import com.tjvib.util.LogUtil;
import com.tjvib.view.activity.DeviceActivity;

import java.util.List;

public class DevicePresenter extends BasePresenter {

    private static final String TAG = "DevicePresenter";

    MyResultReceiver resultReceiver;

    DeviceActivity view;

    LpmsB2SensorService mService;

    boolean bound;

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LpmsB2SensorService.LocalBinder binder = (LpmsB2SensorService.LocalBinder) service;
            mService = (LpmsB2SensorService) binder.getService();
            bound = true;
            mService.startBtDiscovery();
            view.dismissInitDialog();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    public void startOrBindToService() {
        if (LpmsB2SensorService.isRunning) {
            LogUtil.d("isRunning");
            bindToService();
        } else {
            LogUtil.d("!isRunning");
            Intent intent = new Intent(view, LpmsB2SensorService.class);
            intent.putExtra("receiver", resultReceiver);
            view.startService(intent);
            bindToService();
        }
    }

    public void bindToService() {
        Intent intent = new Intent(view, LpmsB2SensorService.class);
        view.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        LogUtil.d("bindToService");
    }

    public void unbindFromService() {
        if (bound) {
            view.unbindService(mConnection);
            bound = false;
        }
    }

    public void stopService() {
        if(mService != null) mService.disconnectAllSensor();
        Intent intent = new Intent(view, LpmsB2SensorService.class);
        view.stopService(intent);
    }

    public DevicePresenter(DeviceActivity view) {
        this.view = view;
        resultReceiver = new MyResultReceiver(null);
    }

    @Override
    String getTag() {
        return TAG;
    }

    public void connectSensor(String address, Callback callback) {
        mService.connectSensor(address, callback);
    }

    public void disconnectSensor(String address, Callback callback) {
        mService.disconnectSensor(address, callback);
    }

    public void disconnectAllSensor() {
        if(mService != null) mService.disconnectAllSensor();
    }

    public List<SensorInfo> getSensorInfoList() {
        return mService.getLpmsB2SensorInfoList();
    }

    public int getConnectedSensorsSize() {
        return mService.getConnectedSensorsSize();
    }

    public void startDiscovering() {
        mService.startBtDiscovery();
    }

    public void stopDiscovering() {
        mService.stopBtDiscovery();
    }

    public boolean isDiscovering() {
        return mService.isBtDiscovery();
    }

    @SuppressLint("ParcelCreator")
    class MyResultReceiver extends ResultReceiver {
        public MyResultReceiver(Handler handler) {
            super(handler);
        }

        @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {
            if (resultCode == SensorService.MSG_SENSOR_CONNECTED) {
                String msg = resultData.getString(SensorService.KEY_MESG);
                showToastOnUI(msg);
            } else if (resultCode == SensorService.MSG_SENSOR_DISCONNECTED) {
                String msg = resultData.getString(SensorService.KEY_MESG);
                showToastOnUI(msg);
            } else if (resultCode == SensorService.MSG_SENSOR_CONNECTION_ERROR) {
                String msg = resultData.getString(SensorService.KEY_MESG);
                showToastOnUI(msg);
            } else {
                //showToast("yay");
            }
        }

        void showToastOnUI(String msg) {
            final String lMsg = msg;
            Handler h = new Handler(view.getMainLooper());
            // Although you need to pass an appropriate context
            h.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(view, lMsg, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

}
